package com.tjsinfo.tjsoa.articles.bo;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.util.MessageResources;
import org.hibernate.criterion.DetachedCriteria;

import com.tjsinfo.tjsoa.articles.vo.TIndexArticles;

//import com.tjsinfo.tjsoa.common.page.PageBean;

/**
 * 数据管理Action父类
 * @author 天津市电子计算机研究所
 *
 */
public class ManageSuperAction extends DispatchAction{

//	protected Integer page;
//	protected Integer limit;
//	protected Integer totalPage;
//	protected PageBean pageBean;
//	protected String  orderField;
//	protected String  orderMode;
	protected final String READONLEY="readonley";
	protected final String SHOWMESSAGE="showmessage";
	protected final String TOADDOK="toaddok";
	protected final String TOUPDATEOK="toupdateok";
	protected final String OPENLIST="openlist";
	protected final String OPENVIEW="openview";
	protected final String FAILED="failed";
	protected final String LINK="link";
	protected final String SUBSECTION="subsection";
	protected final String GSDT="gsdt";	
	protected final String OPENBIGVIEW="openbigview";
	protected final String TOADDRules="toaddrules";
	protected final String TOUPDATERules="toupdaterules";
	
//	public String getOrderField() {
//		return orderField;
//	}
//
//	public void setOrderField(String orderField) {
//		this.orderField = orderField;
//	}
//
//	public String getOrderMode() {
//		return orderMode;
//	}
//
//	public void setOrderMode(String orderMode) {
//		this.orderMode = orderMode;
//	}

	public ManageSuperAction() {
		super();
	}

//	public Integer getPage() {
//		return page;
//	}
//
//	public void setPage(Integer page) {
//		this.page = page;
//	}
//
//	public Integer getLimit() {
//		return limit;
//	}
//
//	public void setLimit(Integer limit) {
//		this.limit = limit;
//	}

	/**
	 * 实现action接口定义的方法

	 * @return 返回相应类型
	 * @throws Exception
	 */
	
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

//	public Integer getTotalPage() {
//		return totalPage;
//	}
//
//	public void setTotalPage(Integer totalPage) {
//		this.totalPage = totalPage;
//	}
	
	/**
     * 截取字符串设定长度
     * @param str
     * @param cutCount 设定长度，字节数
     * @return
     */
    public static String getSubStr(String str, int cutCount) {
        if (str == null)
            return "";

        String resultStr = "";
        char[] ch = str.toCharArray();
        int count = ch.length;
        int strBLen = str.getBytes().length;
        int temp = 0;
        for (int i = 0; i < count; i++) {
            resultStr += ch[i];
            temp = resultStr.getBytes().length;
            if (temp >= cutCount && temp < strBLen) {
                resultStr += "";
                break;
            }
        }
        return resultStr;
    }
    

	/**
	 * 从配置文件messageResource.properties中获取html文件上传路径
	 * @return
	 */
	public String getFilePath(HttpServletRequest request)
	{ 
		
		MessageResources message=getResources(request);
		String strFilePath=message.getMessage(getLocale(request),"filepath");
//		Properties props = new Properties();
//	    ClassLoader cl = this.getClass().getClassLoader();
//		InputStream is = cl.getResourceAsStream("com/tjsinfo/tjsoa/articles/properties/messageResource.properties"); 
//		try {
//			props.load(is);
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		} 
//		String strFilePath = props.getProperty("filepath");
		return strFilePath;
	}	
	
	public String getImgServer (HttpServletRequest request, String serverName)
	{
		MessageResources message=getResources(request);
		return message.getMessage(getLocale(request),serverName);
		
	}
	
	public void uploadHtmlFile(String uuid,String content,HttpServletRequest request)
	{
		String dirName = getFilePath(request);  //获取上传路径
		FileOutputStream o;
		try {
			o = new FileOutputStream(dirName+"\\"+uuid+".html");
		//	content = "<link href=\"../articlesCss/css.css\" rel=\"stylesheet\" type=\"text/css\" />"+content;
			o.write(content.getBytes("UTF-8"));
		    o.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 根据articles_uuid读取html正文
	 * * @param articles_uuid
	 * @return

	public String downloadHtmlFile(String articles_uuid,HttpServletRequest request)
	{
		FileInputStream fis;
		String content = "";
		try {
			String dirName = getFilePath(request);  //获取上传路径	
			fis = new FileInputStream( dirName +"\\"+ articles_uuid +".html");
			InputStreamReader o = new InputStreamReader(fis,"utf-8");
			String str = "";
			int nBuffer = -1;   
			while((nBuffer = o.read())!= -1)   
			{
				str+=(char)nBuffer;
			}
			content = str;
//			if(content!=null && !"".equals(content)){
//				if (content.indexOf("articlesCss/css.css")!=-1) {
//					content = content.replace("<link href=\"../articlesCss/css.css\" rel=\"stylesheet\" type=\"text/css\" />","");
//				}
//			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return content;
	}	 */
	
	 /**  
     * @param filePath 文件路径  
     * @return 获得html的全部内容  
     */  
    public String downloadHtmlFile(String articles_uuid,HttpServletRequest request) {   
        BufferedReader br=null;   
        String dirName = getFilePath(request);  //获取上传路径	
        StringBuffer sb = new  StringBuffer();   
        try {   
            br=new BufferedReader(new InputStreamReader(new FileInputStream( dirName +"\\"+ articles_uuid +".html"),  "UTF-8"));            
            String temp=null;          
            while((temp=br.readLine())!=null){   
                sb.append(temp);   
            } 
            
        } catch (FileNotFoundException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        return sb.toString();   
    }   
	
	
	
	
}
